<?php
namespace app\api\controller;

use app\api\model\Banner;
use app\api\model\NewsType;
use app\api\model\User;
use app\api\model\News;
use app\api\model\Order;
use think\Config;

class Index extends Auth
{
    public function index()
    {

        $aa = Config::get('web.pic_url')['banner'];
        // dump($aa);exit;
        echo 11;
        // return $aa;
    }

    /**
	 * 轮播图
	 * @author dong
	*/
    public function banner()
    {
    	$list = Banner::where('status', 1)->order('id DESC')->field('id, title, pic, weixin_url')->select();

        return success($list, '数据获取成功');
    }

    /**
	 * 首页头部菜单
	 * @author dong
	*/
    public function nav()
    {
    	$map = [];
    	$map['pid'] = 0;
    	$map['is_nav_show'] = 1;
    	$map['status'] = 1;
    	$list = NewsType::where($map)->order('sort ASC')->field('id, nav_title, modular, weixin_icon')->limit(3)->select();

        return success($list, '数据获取成功');
    }

    /**
     * 首页推荐用户  默认显示10条数据
     * @author dong
    */
    public function recommend()
    {
        $list = User::alias('u')
            ->where('u.status', 1)
            ->where('a.status', 1)
            ->join('jk_user_informations m', 'u.user_id = m.user_id', 'LEFT')
            ->join('jk_user_authentications a', 'a.user_id = u.user_id', 'LEFT')
            ->join('jk_user_levels l', 'm.integral >= l.min_integral && m.integral <= l.max_integral', 'LEFT')
            ->limit(10)
            ->field('u.user_id, m.nickname, m.user_icon_url, m.descs, a.type, l.name as level_name, l.pic as level_pic, u.user_id as xinyu')
            ->order('xinyu DESC')
            ->select();

        // dump(json_decode( json_encode( $list),true));exit;
        return success($list, '数据获取成功');
    }

    /**
     * 全部用户
     * @author dong
    */
    public function user_list()
    {
        $map = [];
        if (!empty($this->request->param('key'))) {
            $map['m.nickname'] = array('like', "%{$this->request->param('key')}%");
        }
        $list = User::alias('u')
            ->where($map)
            ->join('jk_user_informations m', 'u.user_id = m.user_id', 'LEFT')
            ->join('jk_user_levels l', 'm.integral >= l.min_integral && m.integral <= l.max_integral', 'LEFT')
            ->join('jk_user_authentications a', 'a.user_id = u.user_id', 'LEFT')
            ->field('u.user_id, m.nickname, m.user_icon_url, m.descs, a.type, l.name as level_name, l.pic as level_pic, u.user_id as xinyu')
            ->paginate($this->number);
        // dump(json_decode( json_encode( $list),true));exit;
        $list = formatPage('userItem', $list);

        return success($list, '数据获取成功');
    }

    /**
     * 查看用户主页
     * @author dong
    */
    public function user_home($user_id='')
    {
        if(empty($user_id) || $user_id == ''){
            return error('请传用户的ID');
        }

        $detail = User::alias('u')
            ->where('u.user_id', $user_id)
            ->join('jk_user_informations m', 'm.user_id = u.user_id', 'LEFT')
            ->join('jk_user_levels l', 'm.integral >= l.min_integral && m.integral <= l.max_integral', 'LEFT')
            ->field('u.user_id, u.user_id as user_icon_url, m.nickname, m.descs, m.integral, m.praise, m.commonly, m.negative_comment, l.name as level_name, l.pic as level_pic')
            ->find();
        
        $detail['popularity'] = News::where('user_id', $user_id)->sum('zan');   //人气数: 指的是发布帖子获得所有的点赞数
        $detail['news_count'] = News::where('user_id', $user_id)->count();  //文章总数
        $detail['order_count'] = Order::where('status', 4)->where(['buyer_id | seller_id'=>$user_id,'status'=>['in',[3,4]]])->count();  //成功交易量

        if(empty($detail)){
            return error('该用户不存在');
        }

        return success($detail, '获取用户信息成功');
    }

    /**
     * 查看用户主页--当前用户发表的文章帖子
     * @author dong
    */
    public function user_news($user_id='')
    {
        if(empty($user_id) || $user_id == ''){
            return error('请传用户的ID');
        }
        $map = array(
            'status' => 1,
            'user_id' => $user_id
        );
        $list = News::where($map)->field('id, title, create_at, zan, comment_number, browse_number')->order('create_at DESC')->paginate($this->number);

        $list = formatPage('userNewsItem', $list);

        return success($list, '获取数据成功');
    }
}
